package com.jxtpro.service.setting;

import com.jfinal.kit.Prop;
import com.jfinal.kit.PropKit;
import com.jfinal.kit.Ret;
import com.jxtpro.model.QqSetting;
/**
 * qq 系统设置
 * @author x
 *
 */
public class QQSettingService {
	public static final QQSettingService me = new QQSettingService();
	final QqSetting dao = new QqSetting();
	/**
	 * 查找目前设置的授权登录qq
	 * @return
	 */
	public Ret findSet(){
		return Ret.create("qq", dao.findFirst("select * from qqSetting where id = 1"));
	}
	
	/**
	 * 更新
	 * @param q
	 * @return
	 */
	public Ret update(QqSetting q){
		try{
			q.update();
			return Ret.create("result", "update success");
		}catch(Exception e){
//			log.in()
			return Ret.create("result", "update error");
		}
	}
	/**
	 * 初始化一次
	 * @param q
	 * @return
	 */
	public Ret init(){
		try{
			Prop p = PropKit.use("qq.properties");
			if(!isExistAppid(p.get("appid"))){
				QqSetting q = new QqSetting();
				q.set("appid", p.get("appid"));
				q.set("redirecturi",p.get("redirecturi"));
				q.set("qqLoginBtn",p.get("qqLoginBtn"));
				q.save();
				System.out.println("-------------------------------------\n");
				System.out.println("------ qq setting init success ------\n");
				System.out.println("-------------------------------------");
			}else{
				System.out.println("-------------------------------------\n");
				System.out.println("------ qq setting load success ------\n");
				System.out.println("-------------------------------------\n");
			}
			return Ret.create("result", "------ qq setting init success ------");
		}catch(Exception e){
//			log.in()
			System.out.println("-------------------------------------");
			System.out.println("------ qq setting init error ------");
			System.out.println("-------------------------------------");
			return Ret.create("result", "------ qq setting init error ------");
		}
	}
	/**
	 * 判断qq是否存在
	 * @param appid
	 * @return true 表示存在
	 */
	private boolean isExistAppid(String appid){
		if(dao.findFirst("select appid from qqSetting where appid=?",appid)!=null){
			return true;
		}else{
			return false;
		}
	}
}
